﻿/**  版本信息模板在安装目录下，可自行修改。
* hars_costtotal.cs
*
* 功 能： N/A
* 类 名： hars_costtotal
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2018/1/19 15:41:23   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;

namespace PreeRegisterDAL
{
    /// <summary>
    /// 数据访问类:hars_costtotal
    /// </summary>
    public partial class hars_costtotal
    {
        public hars_costtotal()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("uid", "hars_costtotal");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from hars_costtotal");
            strSql.Append(" where uid=@uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@uid", MySqlDbType.Int32)
            };
            parameters[0].Value = uid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(PreeRegisterModel.hars_costtotal model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into hars_costtotal(");
            strSql.Append("pid,state,PatientCode,HospitaliezdTimes,TotalPrepayMoney,LeftDeposit,TotalMoney,PatientType,Tips,createdate)");
            strSql.Append(" values (");
            strSql.Append("@pid,@state,@PatientCode,@HospitaliezdTimes,@TotalPrepayMoney,@LeftDeposit,@TotalMoney,@PatientType,@Tips,@createdate)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@pid", MySqlDbType.VarChar,50),
                    new MySqlParameter("@state", MySqlDbType.Int32,2),
                    new MySqlParameter("@PatientCode", MySqlDbType.VarChar,50),
                    new MySqlParameter("@HospitaliezdTimes", MySqlDbType.VarChar,5),
                    new MySqlParameter("@TotalPrepayMoney", MySqlDbType.VarChar,200),
                    new MySqlParameter("@LeftDeposit", MySqlDbType.VarChar,200),
                    new MySqlParameter("@TotalMoney", MySqlDbType.VarChar,200),
                    new MySqlParameter("@PatientType", MySqlDbType.VarChar,200),
                    new MySqlParameter("@Tips", MySqlDbType.VarChar,200),
                    new MySqlParameter("@createdate", MySqlDbType.Timestamp)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.state;
            parameters[2].Value = model.PatientCode;
            parameters[3].Value = model.HospitaliezdTimes;
            parameters[4].Value = model.TotalPrepayMoney;
            parameters[5].Value = model.LeftDeposit;
            parameters[6].Value = model.TotalMoney;
            parameters[7].Value = model.PatientType;
            parameters[8].Value = model.Tips;
            parameters[9].Value = model.createdate;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PreeRegisterModel.hars_costtotal model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_costtotal set ");
            strSql.Append("pid=@pid,");
            strSql.Append("state=@state,");
            strSql.Append("PatientCode=@PatientCode,");
            strSql.Append("HospitaliezdTimes=@HospitaliezdTimes,");
            strSql.Append("TotalPrepayMoney=@TotalPrepayMoney,");
            strSql.Append("LeftDeposit=@LeftDeposit,");
            strSql.Append("TotalMoney=@TotalMoney,");
            strSql.Append("PatientType=@PatientType,");
            strSql.Append("Tips=@Tips");
            strSql.Append(" where uid=@uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@pid", MySqlDbType.VarChar,50),
                    new MySqlParameter("@state", MySqlDbType.Int32,2),
                    new MySqlParameter("@PatientCode", MySqlDbType.VarChar,50),
                    new MySqlParameter("@HospitaliezdTimes", MySqlDbType.VarChar,5),
                    new MySqlParameter("@TotalPrepayMoney", MySqlDbType.VarChar,200),
                    new MySqlParameter("@LeftDeposit", MySqlDbType.VarChar,200),
                    new MySqlParameter("@TotalMoney", MySqlDbType.VarChar,200),
                    new MySqlParameter("@PatientType", MySqlDbType.VarChar,200),
                    new MySqlParameter("@Tips", MySqlDbType.VarChar,200),
                    new MySqlParameter("@uid", MySqlDbType.Int32,50)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.state;
            parameters[2].Value = model.PatientCode;
            parameters[3].Value = model.HospitaliezdTimes;
            parameters[4].Value = model.TotalPrepayMoney;
            parameters[5].Value = model.LeftDeposit;
            parameters[6].Value = model.TotalMoney;
            parameters[7].Value = model.PatientType;
            parameters[8].Value = model.Tips;
            parameters[9].Value = model.uid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

      

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_costtotal ");
            strSql.Append(" where uid=@uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@uid", MySqlDbType.Int32)
            };
            parameters[0].Value = uid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string uidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_costtotal ");
            strSql.Append(" where uid in (" + uidlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_costtotal GetModel(int uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uid,pid,state,PatientCode,HospitaliezdTimes,TotalPrepayMoney,LeftDeposit,TotalMoney,PatientType,Tips,createdate from hars_costtotal ");
            strSql.Append(" where uid=@uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@uid", MySqlDbType.Int32)
            };
            parameters[0].Value = uid;

            PreeRegisterModel.hars_costtotal model = new PreeRegisterModel.hars_costtotal();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_costtotal DataRowToModel(DataRow row)
        {
            PreeRegisterModel.hars_costtotal model = new PreeRegisterModel.hars_costtotal();
            if (row != null)
            {
                if (row["uid"] != null && row["uid"].ToString() != "")
                {
                    model.uid = int.Parse(row["uid"].ToString());
                }
                if (row["pid"] != null)
                {
                    model.pid = row["pid"].ToString();
                }
                if (row["state"] != null && row["state"].ToString() != "")
                {
                    model.state = int.Parse(row["state"].ToString());
                }
                if (row["PatientCode"] != null)
                {
                    model.PatientCode = row["PatientCode"].ToString();
                }
                if (row["HospitaliezdTimes"] != null)
                {
                    model.HospitaliezdTimes = row["HospitaliezdTimes"].ToString();
                }
                if (row["TotalPrepayMoney"] != null)
                {
                    model.TotalPrepayMoney = row["TotalPrepayMoney"].ToString();
                }
                if (row["LeftDeposit"] != null)
                {
                    model.LeftDeposit = row["LeftDeposit"].ToString();
                }
                if (row["TotalMoney"] != null)
                {
                    model.TotalMoney = row["TotalMoney"].ToString();
                }
                if (row["PatientType"] != null)
                {
                    model.PatientType = row["PatientType"].ToString();
                }
                if (row["Tips"] != null)
                {
                    model.Tips = row["Tips"].ToString();
                }
                if (row["createdate"] != null && row["createdate"].ToString() != "")
                {
                    model.createdate = DateTime.Parse(row["createdate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uid,pid,state,PatientCode,HospitaliezdTimes,TotalPrepayMoney,LeftDeposit,TotalMoney,PatientType,Tips,createdate ");
            strSql.Append(" FROM hars_costtotal ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM hars_costtotal ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.uid desc");
            }
            strSql.Append(")AS Row, T.*  from hars_costtotal T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod

        #region  ExtensionMethod

        /// <summary>
        /// 根据PID获取费用汇总 查询库里state状态为1的记录
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataSet GetListByPid(string pid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uid,pid,state,PatientCode,HospitaliezdTimes,TotalPrepayMoney,LeftDeposit,TotalMoney,PatientType,Tips,createdate ");
            strSql.Append(" FROM hars_costtotal where 1=1 and state=1 ");
            if (pid.Trim() != "")
            {
                strSql.AppendFormat(" and pid='{0}'", pid);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 根据PID获取费用汇总 查询库里state状态为1的count
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public bool Exists(string pid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from hars_costtotal");
            strSql.Append(" where 1=1 and state=1 and pid=@pid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@pid", MySqlDbType.VarChar,50)
            };
            parameters[0].Value = pid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 根据PID 更新费用汇总表 state状态
        /// </summary>
        /// <param name="pid">pid</param>
        /// <param name="state">state：1：可用 2：不可用</param>
        /// <returns></returns>
        public bool UpdateState(string pid, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_costtotal set ");
            strSql.Append("state=@state ");
            strSql.Append(" where pid=@pid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@state", MySqlDbType.Int32,2),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,50)};
            parameters[0].Value = state;
            parameters[1].Value = pid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

